package cc.lzmsh.handler.security;

import cc.lzmsh.domain.result.Result;
import cc.lzmsh.enums.AppHttpCodeEnum;
import cc.lzmsh.utils.WebUtils;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author:   msh
 * Date:     2024/9/19 下午2:16
 * Description:
 */

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        authException.printStackTrace();
        Result<Object> error = Result.error(AppHttpCodeEnum.LOGIN_ERROR);
        // 需要返回给前端
        WebUtils.renderString(response, JSON.toJSONString(error));
    }
}
